package com.sdust.user.listener;

import com.sdust.model.user.entity.User;
import com.sdust.user.service.UserService;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class UserRabbitMQListener {

    @Autowired
    private UserService userService;

//    @RabbitListener(bindings = @QueueBinding(
//            value = @Queue(name = "testMq"),
//            exchange = @Exchange(name = "testDirectExchangeHeng",type = ExchangeTypes.DIRECT),
//            key = {"testRoutingHeng1","testRoutingHeng2"}
//    ))//默认类型为direct
//    public void queueTestListener(String msg) {
//        System.out.println("消费者testMq收到消息: "+msg);
//    }
//
//    @RabbitListener(bindings = @QueueBinding(
//            value = @Queue(name = "testObjMq"),
//            exchange = @Exchange(name = "testDirectExchangeHengObj",type = ExchangeTypes.DIRECT),
//            key = {"testRoutingHeng3","testRoutingHeng4"}
//    ))//默认类型为direct
//    public void queueTestObjListener(User user) {
//        System.out.println("消费者testObjMq收到消息: "+ user);
//    }

    /**
     * 监听用户信息修改消息队列
     * @param user
     */
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = "UserInfoChangeQueue"),
            exchange = @Exchange(name = "sdust-talk-userInfo-exchange",type = ExchangeTypes.DIRECT),
            key = {}
    ))//默认类型为direct
    public void userInfoChangeListener(User user) {
        System.out.println("消费者用户信息修改userInfoChangeListener收到消息: " + user);

        userService.updateById(user);
    }

}
